package fragments;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

import com.yandex.yandexfotki.R;
import com.yandex.yandexfotki.activities.StartActivity;

import entities.Album;

public class PreviewPhotoFragment extends Fragment {

	public static PreviewPhotoFragment newInstance(Uri photoUri) {
		PreviewPhotoFragment pvf = new PreviewPhotoFragment();
		Bundle arguments = new Bundle();
		arguments.putString("uri", photoUri.toString());
		pvf.setArguments(arguments);
		return pvf;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
//		List<Album> t = ((StartActivity)getActivity()).getYapi().getAlbumsList();
//		selectedAlbum = t.get(0);
//		chAlbum.setAdapter(new ChooseAlbumAdapter((StartActivity)getActivity(), t));
//		chAlbum.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//			@Override
//			public void onItemSelected(AdapterView<?> arg0, View arg1,
//					int arg2, long arg3) {
//				// TODO Auto-generated method stub
//				selectedAlbum = ((StartActivity)getActivity()).getYapi().getAlbumsList().get(arg2);
//			}
//
//			@Override
//			public void onNothingSelected(AdapterView<?> arg0) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.previewphoto, container,false);
		initViews(v);
		return v;
	}
	
	private void initViews(View v){
		ImageView preview = (ImageView)v.findViewById(R.id.preview);
		preview.setImageURI(Uri.parse(getArguments().getString("uri")));
		
		Button send = (Button)v.findViewById(R.id.send);
		send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d("yandex",((StartActivity)getActivity()).getPath(Uri.parse(getArguments().getString("uri"))));
				File image = new File(((StartActivity)getActivity()).getPath(Uri.parse(getArguments().getString("uri"))));
//				File image = new File(getArguments().getString("uri"));
				String id = "none";
				if(selectedAlbum != null) id = selectedAlbum.getId();
				Log.d("yandex",title+" "+description);
				((StartActivity)getActivity()).uploadPhoto(image,id,title,description);
//				getFragmentManager().beginTransaction().remove(PreviewPhotoFragment.this);
				getFragmentManager().popBackStack();
			}
		});
		
		
//		chAlbum = (Spinner)v.findViewById(R.id.choosealbum);
		
		Button addOpt = (Button)v.findViewById(R.id.adddescr);
		addOpt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				addOptions();
			}
		});
	}
	
	public void setOptions(Album selectedAlbum, String title, String description) {
		this.selectedAlbum = selectedAlbum;
		this.title = title;
		this.description = description;
	}
	
	private void addOptions() {
		((StartActivity)getActivity()).startAddOptionsFragment();
	}
	
	String title = "";
	String description = "";
	Spinner chAlbum;
	Album selectedAlbum;
	//private Uri photoUri;
}
